Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3281: Unexpected result when using build args with default values #3449

Merged
merged 3 commits into from
Jun 14, 2016
Merged

Fix #3281: Unexpected result when using build args with default values #3449

merged 3 commits into from
Jun 14, 2016

Conversation

Andrey9kin
Copy link

Fix the issue when build arg is set to None instead of empty string.
Usecase:

cat docker-compose.yml
....
args:
- http_proxy
- https_proxy
- no_proxy

If http_proxy, https_proxy, no_proxy environment variables are not defined
then http_proxy, https_proxy, no_proxy build args will be set to None which breaks all downloads

With this change build args will be set to empty string if corresponding env
variable is not defined

Signed-off-by: Andrey Devyatkin [email protected]

@@ -2121,7 +2121,7 @@ def test_resolve_environment_from_env_file_with_empty_values(self):
'FILE_DEF': u'bär',
'FILE_DEF_EMPTY': '',
'ENV_DEF': 'E3',
'NO_DEF': None
'NO_DEF': ''
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is wrong, the None is the correct value here, so I think the code change isn't correct to fix the issue.

@dnephin dnephin added this to the 1.8.0 milestone May 12, 2016
@aanand
Copy link

aanand commented May 26, 2016

@Andrey9kin Are you up for making the changes @dnephin suggested? We'd like to get this in for 1.8.0!

@Andrey9kin
Copy link
Author

@aanand last few weeks was quite intense so I didn't have time to submit a fix.
I'm planning to submit a patch before end of this week

Fix the issue when build arg is set to None instead of empty string.
Usecase:

cat docker-compose.yml
....
args:
- http_proxy
- https_proxy
- no_proxy

If http_proxy, https_proxy, no_proxy environment variables are not defined
then http_proxy, https_proxy, no_proxy build args will be set to string None which breaks all downloads

With this change build args will not passed to docker engine if they are equal to string None

Signed-off-by: Andrey Devyatkin <[email protected]>
@Andrey9kin
Copy link
Author

@aanand @dnephin new attempt - please take a look. Jenkins seems to fail due to environment flakiness which is unrelated to the change. Can I retrigger Jenkins build somehow?

I'm new to this code base so please advice if there is a better place to filter out those None's

# Moreover it will be sent to the docker engine as None and then
# interpreted as string None which in many cases will fail the build
# That is why we filter out all pairs with value equal to None
buildargs = {k: v for k, v in build_opts.get('args', {}).items() if v != 'None'}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be comparing against the literal value None, not the string 'None'?

@aanand
Copy link

aanand commented May 27, 2016

OK, I've had a look. I think the right place to fix this is at the config parsing stage, rather than in service.py. I've tracked the culprit down to build_string_dict in utils.py, which calls str() on every value in the dictionary, resulting in None -> 'None'.

Here's a fix:

diff --git a/compose/utils.py b/compose/utils.py
index 494beea..6718d5c 100644
--- a/compose/utils.py
+++ b/compose/utils.py
@@ -95,4 +95,6 @@ def microseconds_from_time_nano(time_nano):


 def build_string_dict(source_dict):
-    return dict((k, str(v)) for k, v in source_dict.items())
+    def to_str(value):
+        return '' if value is None else value
+    return dict((k, to_str(v)) for k, v in source_dict.items())

I think the correct thing to do is pass unset args through as the empty string, not omit them entirely. This is consistent with how both environment and interpolation (${VAR}) works. Will this fix your use case?

@Andrey9kin
Copy link
Author

@aanand sure. Would it be Ok if I separate those two fixes into two pull requests so we have good logical division between issues?

@aanand
Copy link

aanand commented May 27, 2016

I think the fix you've currently got to service.py is unnecessary once we've fixed it in utils.py, so I think you can just disregard my code comments and implement it here.

Fix the issue when build arg is set to None instead of empty string. Usecase:

cat docker-compose.yml
.... args:
- http_proxy
- https_proxy
- no_proxy

If http_proxy, https_proxy, no_proxy environment variables are not defined then http_proxy,
https_proxy, no_proxy build args will be set to string None which breaks all downloads

With this change undefined build args will be set to empty string instead of string None

Signed-off-by: Andrey Devyatkin <[email protected]>
@Andrey9kin
Copy link
Author

@aanand thanks for your guidance. I also felt that it should be a way to implement it better. Please take a look on updated version

@dnephin
Copy link

dnephin commented May 27, 2016

LGTM

@@ -95,4 +95,4 @@ def microseconds_from_time_nano(time_nano):


def build_string_dict(source_dict):
return dict((k, str(v)) for k, v in source_dict.items())
return dict((k, str(v if v else '')) for k, v in source_dict.items())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only problem with this is it'll result in the empty string for the integer 0, which probably isn't what the user expects. v if v is not None else '' is probably better. (Would be good to add an arg with a value of 0 to the test, too.)

Fix the issue when build arg is set to None instead of empty string. Usecase:

cat docker-compose.yml
  .... args:
  - http_proxy
  - https_proxy
  - no_proxy

If http_proxy, https_proxy, no_proxy environment variables are not defined then http_proxy,
https_proxy, no_proxy build args will be set to string None which breaks all downloads

With this change undefined build args will be set to empty string instead of string None

Signed-off-by: Andrey Devyatkin <[email protected]>
@Andrey9kin
Copy link
Author

@aanand missed that. Thank you for your comment. Updated accordingly

@shin-
Copy link

shin- commented Jun 3, 2016

LGTM 👍

@Andrey9kin
Copy link
Author

@aanand @dnephin do you have any other comments for this issue? If not shall we get it merged?

@dnephin
Copy link

dnephin commented Jun 14, 2016

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants